<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout='//layouts/column1';
    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu=array();
    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs=array();

    public function createUrl( $route, $params = array(), $ampersand = '&' )
    {
        if ( $route === '' )
            $route = $this->getId() . '/' . $this->getAction()->getId();
        else if ( strpos( $route, '/' ) === false )
            $route = $this->getId() . '/' . $route;
        if ( $route[ 0 ] !== '/' && ( $module = $this->getModule() ) !== null )
            $route = $module->getId() . '/' . $route;
        /*
        * For example the Italian language is selected and you want to choose the type of link is a German /it/de/
        * if you do not use a regular expression
        *
        */
        preg_match( "/\/ru\/|\/en\/|\/ja\/|\/uk\//", $route, $matches ); // identify language to set
        return Yii::app()->createUrl( trim( ( ((count( $matches ) > 0) or key_exists('lang',$params)) ? $route : Yii::app()->language ."/". trim($route,'/') ), '/' ), $params, $ampersand );
    }
    public $currentLanguage;
    public function  __construct($id, $module = null)
        {
        parent::__construct($id, $module);
            $this->currentLanguage = Yii::app()->language;
         if(isset ($_GET['lang']))
            {
                Yii::app()->language=$_GET['lang'];
                //CApplication::setLanguage($_GET['lagn']);
//                print_r();
            }
       }

    static function langWidget(){
        $currentUrl = ltrim(Yii::app()->request->url, '/');
        $currentLanguage = Yii::app()->language;
        if($currentLanguage=='ru')$fullLanguage='Русский';
        if($currentLanguage=='en')$fullLanguage='English';
        if($currentLanguage=='ja')$fullLanguage='Japanese';
        if($currentLanguage=='uk')$fullLanguage='Українська';

        $urlArray=explode('/',$currentUrl);
        if ($urlArray[0]==$currentLanguage){
            unset ($urlArray[0]);
            $urlArray=implode('/',$urlArray);
        }else{
            $urlArray=implode('/',$urlArray);
        }
        $output='
                        <ul>
                            <li><a href="#" class="">
                                    <img class="iclflag" src="/img/'.$currentLanguage.'.png" alt="'.$currentLanguage.'" title="'.$fullLanguage.'">&nbsp;'.$fullLanguage.'</a>
                                <ul>';
        $output.=($currentLanguage!='uk')?'<li class="icl-uk">
                                        <a hreflang="uk" href="/uk/'.$urlArray.'">
                                        <img class="iclflag" src="/img/uk.png" alt="uk" title="Ukrainian">&nbsp;Українська</a>
                                    </li>':'';
        $output.=($currentLanguage!='ru')?'<li class="icl-ru">
                                        <a hreflang="ru" href="/ru/'.$urlArray.'">
                                        <img class="iclflag" src="/img/ru.png" alt="ru" title="Russian">&nbsp;Русский</a>
                                    </li>':'';
        $output.=($currentLanguage!='en')?'                  <li class="icl-en">
                                        <a hreflang="en" href="/en/'.$urlArray.'">
                                        <img class="iclflag" src="/img/en.png" alt="en" title="English">&nbsp;English</a>
                                    </li>':'';
        $output.=($currentLanguage!='ja')?'                  <li class="icl-ja">
                                        <a hreflang="ja" href="/ja/'.$urlArray.'">
                                        <img class="iclflag" src="/img/ja.png" alt="ja" title="Japanese">&nbsp;Japanese</a>
                                    </li>':'';
        $output.='      </ul>
                            </li>
                        </ul>';
        return $output;
    }

    public function redirect($url,$terminate=true,$statusCode=302)
	{
		if(is_array($url))
		{
                        if(isset ($_GET['lang']))
                            {
                                $url['lang'] = $_GET['lang'];
                            }
			$route=isset($url[0]) ? $url[0] : '';
			$url=$this->createUrl($route,array_splice($url,1));
		}
		Yii::app()->getRequest()->redirect($url,$terminate,$statusCode);
	}

    static function recentPropertyes(){
        $model=new Housing;
        $where='fetuared=1';
        $dataProp=$model->getProp(null,3,$where);
        return $dataProp;
    }
} 